<?php
class Material_model extends CI_Model {

    var $table   = 'material';

    function __construct()
    {
        parent::__construct();
    }
    
    function get_list($where = '', $cond = array(), $offset = 0, $limit = 20){
    	if (!empty($where)){
    		$where = 'where ' . $where;
    	}
        $sql = 'select id, multi_id, type, title, author, cover, summary, content, picurl, voiceurl, videourl, media_id, media_id_time, add_time from '
        	. "{$this->table} {$where} order by id desc limit {$offset}, {$limit}";
		$re_obj = $this->db->query($sql, $cond);
		
		if ($re_obj->num_rows() > 0){
			$re = $re_obj->result_array();
			$re_obj->free_result();
		}else {
			$re = array();
		}
		return $re;
    }
    
    function add($items = array()){
    	$sql = "insert into {$this->table} (multi_id, type, title, author, cover, summary, content, picurl, voiceurl, videourl, add_time)"
    		. " values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
    	$this->db->query($sql, $items);
    	return $this->db->insert_id();
    }
    
    function update($items = array(), $where = '', $cond = array()){
    	$_cond = array();
    	foreach ($items as $field => $val){
    		if (!isset($ups)){
    			$ups = "`{$field}`=?";
    		}else {
    			$ups .= ", `{$field}`=?";
    		}
    		$_cond[] = $val;
    	}
    	$_cond = array_merge($_cond, $cond);
    	$where = empty($where) ? '' : 'where ' . $where;
    	$sql = "update {$this->table} set {$ups} {$where}";
    	return $this->db->query($sql, $_cond);
    }
    
    function del($where = '', $cond = array()){
    	if (empty($where)){
    		return false;
    	}
    	$sql = "delete from {$this->table} where {$where}";
    	return $this->db->query($sql, $cond);
    }
    
    function up_file($file, $mtype = 1){
    	$re = true;
    	$msg = $file_name = $file_path = '';
    	$nowtime = time();
    	if ($file['error'] > 0){
    		$re = false;
    		switch ($file['error']){
    			case 1:
    				$msg = '文件大小超过服务器限制';
    				break;
    			case 2:
    				$msg = '文件太大！';
    				break;
    			case 3:
    				$msg = '文件只加载了一部分！';
    				break;
    			case 4:
    				$msg = '文件加载失败！';
    				break;
    		}
    	}else {
    		$base_path = dirname(dirname(dirname(__FILE__))) . '/htdocs';
    		$file_path = '/upload/material/' . $mtype . date('/Y/md/', $nowtime);
    		if (!file_exists($base_path.$file_path)){
    			$mkre = mkdir($base_path.$file_path, 0755, true);
    			if (!$mkre){
    				$msg = '创建文件目录失败！';
    				$re = false;
    				return array('re' => $re, 'msg' => $msg);
    			}
    		}
    			
    		$file_name = date('His_', $nowtime) . mt_rand(1000, 9999) . '_' . $file['name'];
    		if(is_uploaded_file($file['tmp_name'])){
    			if(!move_uploaded_file($file['tmp_name'], $base_path . $file_path . $file_name)){
    				$msg = '移动文件失败！';
    				$re = false;
    			}
    		} else{
    			$msg = '上传文件失败！';
    			$re = false;
    		}
    	}
    	return array('re' => $re, 'msg' => $msg, 'file' => $file_path . $file_name);
    }
	
	function upload_to_wx($url, $file = '', $type = ''){
		if (!empty($file) && file_exists($file)){
			$fileinfo = pathinfo($file);
			$filetype = $type . '/' . (isset($fileinfo['extension']) ? $fileinfo['extension'] : '');
			//$args['media'] = new CurlFile($file, $filetype, $fileinfo['basename']);
			$args['media'] = class_exists('CurlFile', false) ? new CurlFile($file, $filetype, $fileinfo['basename']) : "@{$file}";
		}else{
			return false;
		}
		
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($curl, CURLOPT_POST, 1);
		curl_setopt($curl, CURLOPT_POSTFIELDS, $args);
		curl_setopt($curl, CURLOPT_INFILESIZE, filesize($file));
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$output = curl_exec($curl);
		curl_close($curl);
		return $output;
	}
	
	function check_mediaid_wx($media_id){
		
	}

}
